package service;

import entity.ReaderType;
import function.Druid;
import function.ReaderTypeDaoImpl;
import interfaces.ReaderTypeServiceDao;
import tool.TSUtility;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class ReaderTypeServiceDaoImpl implements ReaderTypeServiceDao {
    ReaderTypeDaoImpl readerTypeDao = ReaderTypeDaoImpl.getReaderTypeDao();
    @Override
    public void insert() {

        Connection connection = null;
        try {
            connection = Druid.getConnection();
            System.out.println("请输入你要添加的读者类型名字");
            String name = TSUtility.readKeyBoard(4, false);
            System.out.println("请输入该读者最大借书数量");
            int maxborrownum = TSUtility.readInt();
            System.out.println("请输入该读者最大借书天数");
            int maxlimit = TSUtility.readInt();
            System.out.println("确认是否添加Y/N");
            char c = TSUtility.readConfirmSelection();
            if (c == 'Y') {
                readerTypeDao.insert(connection, new ReaderType(name, maxborrownum, maxlimit));
                System.out.println("输入成功");
            } else if (c == 'N') {
                System.out.println("退出成功");
            }

            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteById() {
        ReaderTypeDaoImpl readerTypeDao = ReaderTypeDaoImpl.getReaderTypeDao();
        try {
            System.out.println("--------已拥有的读者类型---------");
            getAll();
            System.out.println("-------------------------------");
            Connection connection = Druid.getConnection();
            System.out.println("请输入你删除的读者类型编号");
            int id = TSUtility.readInt();
            System.out.println("确认是否删除Y/N");
            char c = TSUtility.readConfirmSelection();
            if (c == 'Y') {

                ReaderType readerType = updateReturn(id);
                if (readerType == null) {
                    System.out.println("此读者类型不存在。请前往添");
                } else {
                    readerTypeDao.deleteById(connection, id);
                }

            } else if (c == 'N') {
                System.out.println("退出成功");
            }
            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update() {
        ReaderTypeDaoImpl readerTypeDao = ReaderTypeDaoImpl.getReaderTypeDao();
        try {

            Connection connection = Druid.getConnection();
            System.out.println("--------已拥有的读者类型---------");
            getAll();
            System.out.println("-------------------------------");


            System.out.println("输入你要修改的编号");
            int i = TSUtility.readInt();
            int returnID = i;
            ReaderType readerType = updateReturn(returnID);
            if (readerType == null) {
                System.out.println("没有该读者类型，请先前往添加");
                return;
            }
            System.out.println("输入你要修改的读者类型名字——" + readerType.getTypename());
            String name = TSUtility.readString(4, readerType.getTypename());
            System.out.println("输入你要修改的该读者可借阅的最大数量——" + readerType.getMaxborrownum());
            int maxborrownum = Integer.parseInt(TSUtility.readString(2, readerType.getMaxborrownum() + ""));
            System.out.println("输入你要修改的该读者可借阅的最大数量——" + readerType.getReaderlimit());
            int readerlimit = Integer.parseInt(TSUtility.readString(2, readerType.getReaderlimit() + ""));
            readerType.setTypename(name);
            readerType.setMaxborrownum(maxborrownum);
            readerType.setReaderlimit(readerlimit);
            readerTypeDao.update(connection, readerType);
            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getReaderTypeById() {
        ReaderTypeDaoImpl readerTypeDao = ReaderTypeDaoImpl.getReaderTypeDao();
        try {
            Connection connection = Druid.getConnection();
            System.out.println("输入你要查询的编号");
            int id = TSUtility.readInt();
            ReaderType customerById = readerTypeDao.getReaderTypeById(connection, id);
            System.out.println(customerById);
            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getAll() {
        ReaderTypeDaoImpl readerTypeDao = ReaderTypeDaoImpl.getReaderTypeDao();
        try {
            Connection connection = Druid.getConnection();
            List<ReaderType> all = readerTypeDao.getAll(connection);
            all.forEach(System.out::println);

            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List judgment() {
        ReaderTypeDaoImpl readerTypeDao = ReaderTypeDaoImpl.getReaderTypeDao();
        Connection connection = null;
        try {
            connection = Druid.getConnection();

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<ReaderType> all = readerTypeDao.getAll(connection);
        Druid.colseResource(connection, null, null);
        return all;

    }

    public ReaderType updateReturn(int id) {
        ReaderTypeDaoImpl readerTypeDao = ReaderTypeDaoImpl.getReaderTypeDao();
        Connection connection = null;
        try {
            connection = Druid.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ReaderType readerType = readerTypeDao.getReaderTypeById(connection, id);
        Druid.colseResource(connection, null, null);
        return readerType;
    }
    public ReaderType determineIfItExists() {
        System.out.println("输入你要修改的编号");
        int i = TSUtility.readInt();
        int returnID = i;
        ReaderType readerType = updateReturn(returnID);
        if (readerType == null) {
            System.out.println("没有该读者类型,重新输入");
            return null;
        }
        return readerType;
    }

    public  ArrayList<ReaderType> judgmentReaderType(ReaderType readerType) throws Exception {
        ArrayList<ReaderType> arr=new ArrayList<>();
        Connection connection = Druid.getConnection();
        List<ReaderType> all = readerTypeDao.getAll(connection);
        for(ReaderType type:all){
            if(type.getReaderlimit()>readerType.getReaderlimit()){
                arr.add(type);
            }
        }
        return arr;
    }
    public static void main(String[] args) {

    }
}